package controlLayer;
import modelLayer.*;
import java.util.ArrayList;

/**
 * Controls persons
 * 
 * @author Gruppe E
 * @version 1.0
 */
public class PersonController
{
    private PersonContainer pCon;

    /**
     * Constructor for objects of class PersonController
     */
    public PersonController()
    {
        pCon = pCon.getInstance();
    }

    /**
     * Method getLoans
     *
     * @param phone A parameter
     * @return ArrayList<Loan>
     */
    public ArrayList<Loan> getLoans(String phone)
    {
        return pCon.getLoans(phone);
    }

    /**
     * Method addLoan
     *
     * @param l A parameter
     * @param phone A parameter
     */
    public void addLoan(Loan l, String phone)
    {
        pCon.addLoan(l, phone);
    }

    /**
     * Method createPerson
     * 
     * creates a person and adds it to the ArrayList persons
     *
     * @param name A parameter
     * @param address A parameter
     * @param zip A parameter
     * @param city A parameter
     * @param phone A parameter
     * @return The return value
     */
    public boolean createPerson(String name, String address, String zip, String city, String phone)
    {
        Person tempP = null;

        if(findPerson(phone) == null)
        {
            tempP = new Person(name, address, zip, city, phone);
            pCon.addPerson(tempP);

            return true;
        }
        else 
        {
            return false;
        }
    }


    /**
     * Returns "confirmed" if the number doesn't exist in the system.
     * Returns "letters" if the number contains letters.
     * Otherwise returns "numbersExists".
     *
     * @param phone Phone number from a friend
     * @return returnString A string that indicates wether the number is in the system or contains letters.
     */
    public String validatePhoneNumber(String phone)
    {        
        String returnString = "numberExists";
        if(findPerson(phone)==null)
        {
            
            returnString = "confirmed";
            //phone er ikke allerede registreret til en person
        }
        
        try
        {
            int phoneInt = Integer.parseInt(phone);
        }
        catch(NumberFormatException e){
            
            returnString = "letters";
            //phone indeholder bogstaver
        }
        return returnString;
        
        //phone er ledigt og indeholder ikke bogstaver
    }

    /**
     * Method findPerson
     * 
     * searches for a person from the given phone number
     *
     * @param phone A parameter
     * @return The return value
     */
    public Person findPerson(String phone)
    {
        return pCon.findPerson(phone);
    }

    /**
     * Method updatePerson
     * 
     * searches for the persons phone number, and updates the persons personal information
     *
     * @param oldPhone A parameter
     * @param name A parameter
     * @param address A parameter
     * @param zip A parameter
     * @param city A parameter
     * @param phone A parameter
     * @return The return value
     */
    public boolean updatePerson(String oldPhone, String name, String address, String zip, String city, String phone)
    {
        if(findPerson(oldPhone) == null)
        {
            return false;
        }
        else
        {
            return pCon.updatePerson(oldPhone, name, address, zip, city, phone);
        }
    }

    /**
     * Method deletePerson
     * 
     * deletes a person that matches the given phone number
     *
     * @param phone A parameter
     * @return The return value
     */
    public boolean deletePerson(String phone)
    {
        return pCon.deletePerson(phone);
    }
}
